﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using NetDon.OA.Data.EntityClasses;
using NetDon.OA.Interface;
using NetDon.OA.Interface.Common;
using NetDon.Utilities;
using NetDon.WebControls;

namespace NetDon.OA.Portal.Admin
{
    [Navigation("系统管理", "调车成本维护")]
    public partial class ShuntingCostManagement : PageBase
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                PageQueryAdapter1.PageSize = 20;
                PageQueryAdapter1.PerformQuery();
            }
        }

        protected override void OnInit(EventArgs e)
        {
            base.OnInit(e);



            ObjectDataSourceRoutes.Selecting += (ds, dsArgs) =>
            {
                dsArgs.InputParameters["areaId"] = CurrentApplicationContext.CompanyId.ToString();
            };
        }

        protected void PageQueryAdapter1_PerformGetCondition(object sender, PerformGetConditionEventArgs e)
        {
            var criteria = new ShuntingCostConfigDataSearchCriteria
            {
                DeliveryCity = this.ucProvinceCity.CityCode,
                RouteTypeId = this.ddlRouteTypeId.SelectedValue.ToGuid(),
                AreaId = CurrentApplicationContext.CompanyId,

                
            };
            if (!string.IsNullOrEmpty(ddlCheckoutType.SelectedValue))
            {
                criteria.CheckoutType = (int)EnumUtilities.Parse<CheckoutType>(ddlCheckoutType.SelectedValue); ;
            }
            e.Condition = criteria;
        }

        protected void PageQueryAdapter1_PerformGetRecordCount(object sender, PerformGetRecordCountEventArgs e)
        {
            var service = ServiceContainer.GetService<IShuntingCostConfigService>();
            e.RecordCount = service.GetShuntingCostConfigCount((ShuntingCostConfigDataSearchCriteria)e.Condition);
        }

        protected void PageQueryAdapter1_PerformGetPageData(object sender, PerformGetPageDataEventArgs e)
        {
            var service = ServiceContainer.GetService<IShuntingCostConfigService>();
            e.DataSource = service.SearchShuntingCostConfigDatasByCondition((ShuntingCostConfigDataSearchCriteria)e.Condition,
                                                                      gridCustomerInfoList.SortExpression,
                                                                      gridCustomerInfoList.SortDirection, e.PageSize,
                                                                      e.PageIndex);
        }

        protected void btnReload_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.ReloadCurrentPage();
        }


        protected void btnSearch_Click(object sender, EventArgs e)
        {
            PageQueryAdapter1.PerformQuery();
        }

        protected void btnDeleteDemand_Click(object sender, EventArgs e)
        {
            var service = ServiceContainer.GetService<IShuntingCostConfigService>();
            var customerId = fieldCustomerId.Value.ToGuid();
            if (service.CheckShuntingCostConfigReferenced(customerId))
            {
                Alert("此调车成本信息在使用中，不能删除！");
                return;
            }

            bool result = service.RemoveShuntingCostConfig(customerId);

            if (result)
            {
                PageQueryAdapter1.PerformQuery();
                Alert("删除调车成本信息成功!");
            }
            else
            {
                Alert("删除调车成本信息失败！");
            }
        }

        protected string GetRouteName(object id)
        {
            if (MiscUtilities.IsNull(id)) return string.Empty;
            var service = ServiceContainer.GetService<IRouteInfoService>();
            return service.GetRouteById(id.ToString().ToGuid()).RouteName;

        }

        protected string GetCityName(object id)
        {
            var cityName = new StringBuilder();
            var service = ServiceContainer.GetService<ILocationService>();
            if (!MiscUtilities.IsNullOrEmpty(id))
            {
                cityName.Append(service.GetCity(id.ToString()).Name).Append(" ");
            }
           
            return cityName.ToString();


        }

    }
}
